/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AltaCuentaProveedores.java
 *
 * Created on 28/07/2011, 21:02:52
 */

package ventanas;

import conexiones.AdministradorDeConexiones;
import dao.CuentaProveedoresDao;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import modelo.CuentaProveedor;
import modelo.Proveedor;

/**
 *
 * @author Cristian Perez
 */
public class AltaCuentaProveedores extends javax.swing.JDialog {
    int tipo;
    Proveedor proveedor;

    /** Creates new form AltaCuentaProveedores */
    public AltaCuentaProveedores(JDialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblCodigo = new javax.swing.JLabel();
        lblRazSocial = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        lblComprobante = new javax.swing.JLabel();
        txtComprobante = new javax.swing.JTextField();
        lblImporte = new javax.swing.JLabel();
        txtImporte = new javax.swing.JTextField();
        lblCodValor = new javax.swing.JLabel();
        lblRazSocValor = new javax.swing.JLabel();
        btnConfirmar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registrar Pago Proveedor");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblCodigo.setFont(new java.awt.Font("Tahoma", 0, 14));
        lblCodigo.setText("Codigo:");

        lblRazSocial.setFont(new java.awt.Font("Tahoma", 0, 14));
        lblRazSocial.setText("Razon Social:");

        jPanel2.setBackground(new java.awt.Color(204, 204, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        lblComprobante.setFont(new java.awt.Font("Tahoma", 0, 14));
        lblComprobante.setText("Comprobante: ");

        txtComprobante.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N

        lblImporte.setFont(new java.awt.Font("Tahoma", 0, 14));
        lblImporte.setText("Importe:");

        txtImporte.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblComprobante, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtComprobante, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblImporte, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(txtImporte, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblComprobante)
                    .addComponent(txtImporte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblImporte)
                    .addComponent(txtComprobante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        lblCodValor.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        lblRazSocValor.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(lblCodigo, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblCodValor, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lblRazSocial, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblRazSocValor, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblRazSocValor, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblCodigo)
                        .addComponent(lblRazSocial))
                    .addComponent(lblCodValor, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19))
        );

        btnConfirmar.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnConfirmar.setText("Confirmar");
        btnConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmarActionPerformed(evt);
            }
        });

        btnCancelar.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(84, Short.MAX_VALUE)
                .addComponent(btnConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(94, 94, 94)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(74, 74, 74))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-514)/2, (screenSize.height-259)/2, 514, 259);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
        // TODO add your handling code here:
        txtComprobante.requestFocus();
    }//GEN-LAST:event_formWindowGainedFocus

    private void btnConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmarActionPerformed
        // TODO add your handling code here:
        if (validarFormulario()) {
            Connection conn = null;
            try {
                conn = AdministradorDeConexiones.obtenerConexion();
                float valor = Float.parseFloat(txtImporte.getText().trim());
                if (tipo == 1) {
                    CuentaProveedor cuentaP = new CuentaProveedor(proveedor, devolverFechaActual(), txtComprobante.getText().trim(), 0, valor);
                    CuentaProveedoresDao.getInstance().insertar(conn, cuentaP);
                }
                else {
                    CuentaProveedor cuentaP = new CuentaProveedor(proveedor, devolverFechaActual(), txtComprobante.getText().trim(), valor, 0);
                    CuentaProveedoresDao.getInstance().insertar(conn, cuentaP);
                }
            }catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(this, "El importe debe ser un numero...", "Error", JOptionPane.WARNING_MESSAGE);
                    txtImporte.requestFocus();
                    txtImporte.setText("");
            }
            catch(Exception e) {e.printStackTrace();}
            JOptionPane.showMessageDialog(this, "La transaccion de la cuenta ah sido guardada", "Guardar", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        }
    }//GEN-LAST:event_btnConfirmarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConfirmar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblCodValor;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblComprobante;
    private javax.swing.JLabel lblImporte;
    private javax.swing.JLabel lblRazSocValor;
    private javax.swing.JLabel lblRazSocial;
    private javax.swing.JTextField txtComprobante;
    private javax.swing.JTextField txtImporte;
    // End of variables declaration//GEN-END:variables

    public void cargarDatosVentana(Proveedor prov, int tipoTransac) {
        //Asignamos el tipo de transaccion a realizar en la cuenta
        tipo = tipoTransac;
        proveedor = prov;
        if (tipo == 1) {
            this.setTitle("Registrar Pago a Proveedor");
        }
        else this.setTitle("Registrar Compra a Proveedor");
        lblCodValor.setText(String.valueOf(prov.getId()));
        lblRazSocValor.setText(prov.getRazSocial());
    }

    private boolean validarFormulario() {
        //Validamos que el campo comprobante no este vacio
        if (txtComprobante.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar un comprobante...", "Error", JOptionPane.WARNING_MESSAGE);
            txtComprobante.requestFocus();
            return false;
        }

        //Validamos que el campo importe no este vacio
        if (txtImporte.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar un importe...", "Error", JOptionPane.WARNING_MESSAGE);
            txtComprobante.requestFocus();
            return false;
        }
        //Se lanza una excepcion si el importe NO es un numero real
        try {
            float valor = Float.parseFloat(txtImporte.getText().trim());
            if (valor <= 0) {
                JOptionPane.showMessageDialog(this, "Debe ingresar un importe valido", "Error", JOptionPane.WARNING_MESSAGE);
                txtImporte.requestFocus();
                return false;
            }
        }catch(Exception NumberFormatException) {
            JOptionPane.showMessageDialog(this, "El importe debe ser un numero...", "Error", JOptionPane.WARNING_MESSAGE);
            txtImporte.requestFocus();
            txtImporte.setText("");
            return false;
        }
        return true;
    }

    private String devolverFechaActual() {
        Date hoy = new Date();
        Calendar actual = Calendar.getInstance();
        SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
        return(sdf.format(hoy));
    }
}
